﻿
using FairyGUI;
using System.Collections.Generic;

namespace GameMenuControlPlatform
{
	public class UIController
    {
		
		public UIController (GComponent view)
		{
			m_view = view;
		}
       
		public virtual void OpenView()
		{
			GRoot.inst.AddChild (m_view);
            Messager.AddListener(GameEvent.UPDATE, Update);//主要是实现每个页面自己的Update
		}
		public virtual void CloseView(bool dispose = true)
        {
            Messager.RemoveListener(GameEvent.UPDATE, Update);
            GRoot.inst.RemoveChild (m_view, dispose);
			if (dispose)
            {
				UIControllerManager.DestroyController (m_name);
			}
		}
		public void SetName(UIControllerName name)
		{
			m_name = name;
        }
        public UnityEngine.Vector2 GetPosition()
        {
            return m_view.position;
        }

        public void TweenPosition(UnityEngine.Vector2 position)
        {
            m_view.TweenMove(position, 2f);
        }
        public void SetPosition(float x, float y)
        {
            m_view.SetPosition(x, y, 0);
        }
        public UnityEngine.Vector2 GetScale()
        {
            return m_view.scale;
        }
        public void SetScale(float x, float y)
        {
            m_view.SetScale(x, y);
        }
        public void SetAngle(float angle) {
            m_view.TweenRotate(angle,0.1f);
        }
        internal void SetSortingOrder(int i)
        {
            m_view.sortingOrder = i;
        }
        public void SetVisible(bool visible)
        {
            m_view.visible = visible;
        }

        public virtual void Update()
        {

        }

        protected void AddChild(UIController child) 
        {
            m_children.Add(child);
        }
        protected void RemoveChild(UIController child)
        {
            m_children.Remove(child);
        }
        protected void ClearChildren()
        {
            m_children.Clear();
        }
        protected GComponent m_view;
		protected UIControllerName m_name;
        protected List<UIController> m_children = new List<UIController>();
	}
}
